package br.com.portal.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.portal.beans.Vendedor;
import br.com.portal.dao.VendedorDAO;

@WebServlet("/logar")
public class Logar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Logar() {
        super();

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter resposta = response.getWriter();
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");

		try {
			Vendedor vendedor = VendedorDAO.logar(login, senha);
			if(vendedor == null)
				resposta.print("{success: false, motivo: 'Usu&aacute;rio ou senha inv&aacute;lido(s)!'}");
			else
				resposta.print("{success: true, motivo: 'ok'}");
		} catch (Exception e) {
			resposta.print("{success: false, motivo: 'Falha interna! contate o administrador do sistema: " + e.getMessage() + "'}");
			e.printStackTrace();
		}
	}
}
